using System;
using System.Data.OleDb;
using DTO;


namespace DAO
{
    public class HoaDonBanDao:IDaoObject<HoaDonBanDto, int>
    {
        #region IDaoObject<HoaDonBanDto,int> Members
        //public HoaDonBanDto SaveOrUpdate(HoaDonBanDto obj)
        //{
        //    OleDbConnection connection = MyConnection.ConnectionData();
        //    string sqlPattern = "insert into Hoadon(Mahoadon, Manhanvien, Ngaytao, Tongtien, Vat, Sotienthanhtoan, Loaihoadon)" +
        //                         " values ('{0}','{1}','{2}','{3}',{4},{5},'{6}')";
        //    string sqlString1 = String.Format(sqlPattern, obj.MaHoaDon, obj.IdNhanVien, obj.NgayTao,
        //                                              obj.TongTien, obj.Vat, obj.SoTienThanhToan, 0);
        //    OleDbCommand cmd = new OleDbCommand(sqlPattern, connection);
        //    //int hoadonId = cmd.ExecuteNonQuery();
        //    cmd.ExecuteNonQuery();
        //    sqlPattern = "Select @@IDENTITY";
        //    OleDbCommand cmd1 = new OleDbCommand(sqlPattern, connection);
        //    obj.Id = (int) cmd.ExecuteScalar();
        //    //obj.Id = hoadonId;
        //    return obj;
        //}

        public HoaDonBanDto SaveOrUpdate(HoaDonBanDto obj)
        {
            OleDbConnection cn = MyConnection.ConnectionData();
            if(obj.Id>0)
            {
                string strSQL = "Update Hoadon Set Mahoadon=?, Manhanvien=?, Ngaytao=?, Tongtien=?, Vat=?, Sotienthanhtoan=?, Loaihoadon=? Where Id=?";
                OleDbCommand cmd = new OleDbCommand(strSQL, cn);
                cmd.Parameters.Add("@Mahoadon", OleDbType.WChar);
                cmd.Parameters["@Mahoadon"].Value = obj.MaHoaDon;
                cmd.Parameters.Add("@Manhanvien", OleDbType.Integer);
                cmd.Parameters["@Manhanvien"].Value = obj.MaNhanVien;
                cmd.Parameters.Add("@Ngaytao", OleDbType.Date);
                cmd.Parameters["@Ngaytao"].Value = obj.NgayTao;
                cmd.Parameters.Add("@Tongtien", OleDbType.Integer);
                cmd.Parameters["@Tongtien"].Value = obj.TongTien;
                cmd.Parameters.Add("@Vat", OleDbType.Integer);
                cmd.Parameters["@Vat"].Value = obj.Vat;
                cmd.Parameters.Add("@Sotienthanhtoan", OleDbType.Integer);
                cmd.Parameters["@Sotienthanhtoan"].Value = obj.SoTienThanhToan;
                cmd.Parameters.Add("@Loaihoadon", OleDbType.Integer);
                cmd.Parameters["@Loaihoadon"].Value = obj.LoaiHd;
                cmd.Parameters.Add("@Id", OleDbType.Integer);
                cmd.Parameters["@Id"].Value = obj.Id;
                cmd.ExecuteNonQuery();
                cn.Close();
                return obj;
            }
            else
            {
                string strSQL = "Insert into Hoadon(Mahoadon, Manhanvien, Ngaytao, Tongtien, Vat, Sotienthanhtoan, Loaihoadon) values(?,?,?,?,?,?,?)";
                OleDbCommand cmd = new OleDbCommand(strSQL, cn);
                cmd.Parameters.Add("@Mahoadon", OleDbType.WChar);
                cmd.Parameters["@Mahoadon"].Value = obj.MaHoaDon;
                cmd.Parameters.Add("@Manhanvien", OleDbType.Integer);
                cmd.Parameters["@Manhanvien"].Value = obj.MaNhanVien;
                cmd.Parameters.Add("@Ngaytao", OleDbType.Date);
                cmd.Parameters["@Ngaytao"].Value = obj.NgayTao;
                cmd.Parameters.Add("@Tongtien", OleDbType.Integer);
                cmd.Parameters["@Tongtien"].Value = obj.TongTien;
                cmd.Parameters.Add("@Vat", OleDbType.Integer);
                cmd.Parameters["@Vat"].Value = obj.Vat;
                cmd.Parameters.Add("@Sotienthanhtoan", OleDbType.Integer);
                cmd.Parameters["@Sotienthanhtoan"].Value = obj.SoTienThanhToan;
                cmd.Parameters.Add("@Loaihoadon", OleDbType.Integer);
                cmd.Parameters["@Loaihoadon"].Value = obj.LoaiHd;
                cmd.ExecuteNonQuery();
                strSQL = "Select @@IDENTITY";
                cmd = new OleDbCommand(strSQL, cn);
                obj.Id = (int)cmd.ExecuteScalar();
                cn.Close();
                return obj;
            }
            
        }

        public HoaDonBanDto SaveOrUpdate(HoaDonBanDto obj, OleDbTransaction transaction)
        {
            throw new NotImplementedException();
        }

        public void Delete(int id)
        {
            OleDbConnection cn = MyConnection.ConnectionData();
            string strSql = "Delete from Hoadonban where ID = '"+id+"' ";
            OleDbCommand cmd = new OleDbCommand(strSql,cn);
            
            cmd.ExecuteNonQuery();
            cn.Close();
            return;
        }

        public HoaDonBanDto Select(int id)
        {
            //OleDbConnection cn = MyConnection.ConnectionData();
            //string strSql = "select * from Hoadonban where ID = '"+id+"' ";
            //OleDbCommand cm = new OleDbCommand(strSql, cn);
            return null;
        }

        public HoaDonBanDto[] SelectAll()
        {
            //throw new NotImplementedException();
            return new HoaDonBanDto[] {};
        }

        #endregion
    }
       
}
